using TodoPro.Utility;
using UnityEditor;
using NotImplementedException = System.NotImplementedException;

namespace TodoPro.Editor
{
    public class EditorPrefsStorage : IStorage
    {
        public void SaveInt(string key, int value)
        {
            EditorPrefs.SetInt(key,value);
        }

        public int LoadInt(string key, int defaultValue = 0)
        {
            return EditorPrefs.GetInt(key, defaultValue);
        }

        public void SaveObject(string key, object obj)
        {
            var jsonSerializer = TodoProConfig.GetUtility<IJsonSerializer>();
            var objectJsonString = jsonSerializer.Serialize(obj);
            EditorPrefs.SetString(key, objectJsonString);
        }

        public T LoadObject<T>(string key, T defaultValue = default(T))
        {
            var jsonSerializer =  TodoProConfig.GetUtility<IJsonSerializer>();

            var objectJsonString = EditorPrefs.GetString(key, string.Empty);

            if (string.IsNullOrEmpty(objectJsonString))
            {
                return defaultValue;
            }
            else
            {
                var obj = jsonSerializer.Deserialize<T>(objectJsonString);

                if (obj != null)
                {
                    return obj;
                }
                else
                {
                    return defaultValue;
                }
            }
            
        }
    }
}